package com.hc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hc.common.bean.PageBean;
import com.hc.domain.User;
import com.hc.mapper.UserMapper;
import com.hc.query.UserVO;
import com.hc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: hecai
 * @date: 2021/11/9 11:17
 * @description:
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        log.info("### 插入成功 {} ###", record);
        return userMapper.insert(record);
    }

    @Override
    public int updateUserStatusByCode(String code) {
        int updateRes =  userMapper.updateUserStatusBySalt(code);
        return updateRes;
    }
    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageBean<User> selectPageByCondition(Integer pageNum, Integer pageSize, UserVO userVO) {
        Page<User> page = PageHelper.startPage(pageNum, pageSize);
        userMapper.selectUsersByCondition(userVO);
        PageBean<User> pageBean = PageBean.init(page);
        return pageBean;
    }

    @Override
    public int activeByPrimaryKey(Long id) {
        return userMapper.avctiveByPrimaryKey(id);
    }

    @Override
    public PageBean<User> selectPageHasDelete(Integer pageNum, Integer pageSize, UserVO userVO) {
        Page<User> page = PageHelper.startPage(pageNum, pageSize);
        userMapper.selectUsersHasDelete(userVO);
        PageBean<User> pageBean = PageBean.init(page);
        return pageBean;
    }

    @Override
    public int disabledByPrimaryKey(Long id) {
        return userMapper.disabledByPrimaryKey(id);
    }

    @Override
    public PageBean<User> selectPageByConditionAnother(Integer pageNum, Integer pageSize, UserVO userVO) {
        Page<User> page = PageHelper.startPage(pageNum, pageSize);
        userMapper.selectUsersByConditionAnother(userVO);
        PageBean<User> pageBean = PageBean.init(page);
        return pageBean;
    }

    @Override
    public User selectByAccount(String account) {
        return userMapper.selectByAccount(account);
    }



    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }


    @Override
    public boolean isAccountExists(String account) {
        UserVO userVO = UserVO.builder()
                .account(account)
                .build();
        final List<User> users = userMapper.selectUsersByCondition(userVO);
        return users.size() > 0 ? true : false;
    }

    @Override
    public User selectBySalt(String salt) {

        return userMapper.selectBySalt(salt);

    }

    @Override
    public int deleteInactivatedByPrimaryKey(Long id) {
        return userMapper.deleteInactivatedByPrimaryKey(id);
    }
}